package com.florent37.runtimepermission3.slice;

import com.florent37.runtime_permission.RuntimePermission;
import com.florent37.runtimepermission3.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.bundle.IBundleManager;
import com.florent37.runtimepermission3.AppendText;
import ohos.security.permission.Permission;

import java.util.ArrayList;


public class RuntimePermissionMainActivityJava8Slice extends AbilitySlice implements Permission.OnRequestPermissionsResult {
    private final static String WRITE_USER_STORAGE = "ohos.permission.WRITE_USER_STORAGE";
    private final static String LOCATION = "ohos.permission.LOCATION";



    private Text resultText;
    private Component requestComponent;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_fragment_runtime_fragment_in);

        /*if (verifySelfPermission(PERMISSION_READ_CONTEXTS) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(
                    new String[]{"ohos.permission.WRITE_USER_STORAGE",LOCATION}, 123);

        }else {
            return;
        }*/

        resultText = (Text) findComponentById(ResourceTable.Id_resultView);
        requestComponent = findComponentById(ResourceTable.Id_requestView);

        ArrayList list = new ArrayList();
        list.add(WRITE_USER_STORAGE);
        list.add(LOCATION);
        requestComponent.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent newIntent = new Intent();
                newIntent.setStringArrayListParam("getPermission",list);
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.florent37.runtimepermission3")
                        .withAbilityName("com.florent37.runtime_permission.AskPermission")
                        .build();
                newIntent.setOperation(operation);
                startAbility(newIntent);
            }
        });
    }

    private void myMethod() {

        RuntimePermission.askPermission((FractionAbility) RuntimePermissionMainActivityJava8Slice.this.getAbility())
                .request("ohos.permission.WRITE_USER_STORAGE", LOCATION)


                .onAccepted((result) -> {
                    System.out.println("----------into onAccepted-----------");
                    //all permissions already granted or just granted

                    //your action
                    resultText.setText("Accepted :" + result.getAccepted());
                })
                .onDenied((result) -> {
                    System.out.println("----------into onDenied-----------");
                    AppendText.appendText(this, resultText, "Denied :");
                    //the list of denied permissions
                    for (String permission : result.getDenied()) {
                        AppendText.appendText(this, resultText, permission);
                    }
                    //permission denied, but you can ask again, eg:

                    new CommonDialog(this)
                            .setContentText("Please accept our permissions")
                            .setButton(0, "yes", (dialog, which) -> {
                                result.askAgain();
                            }) // ask again
                            .setButton(1, "no", (dialog, which) -> {
                                dialog.destroy();
                            })
                            .show();

                })
                .onForeverDenied((result) -> {
                    System.out.println("----------into onForeverDenied-----------");
                    AppendText.appendText(this, resultText, "ForeverDenied :");
                    //the list of forever denied permissions, user has check 'never ask again'
                    for (String permission : result.getForeverDenied()) {
                        AppendText.appendText(this, resultText, permission);
                    }
                    // you need to open setting manually if you really need it
                    result.goToSettings();
                })
                .ask();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onResult(String s, String[] strings, int[] ints) {

    }

    @Override
    public void onCancel(String s, String[] strings) {

    }

    @Override
    public void onTimeOut(String s, String[] strings) {

    }
}
